/*
 * SoundLoader.cpp
 *
 *  Created on: 03.11.2013
 *      Author: dima
 */

#include "../Globals.hpp"

#include "SoundLoader.hpp"



Tanks::Resources::SoundLoader &
Tanks::
Resources::
SoundLoader::
getInstance()
{
  static SoundLoader instance;
  return instance;
}


Tanks::
Resources::
SoundLoader::
SoundLoader() : m_maxChannels(50)
{
  Mix_AllocateChannels(m_maxChannels);
  for(int i = m_maxChannels - 1; i >= 0; --i)
  {
    m_freeChannels.push_back(i);
  }
}


Tanks::
Resources::
SoundLoader::
~SoundLoader()
{
  for(unsigned int soundIter = 0; soundIter < m_sounds.size(); ++soundIter)
  {
    Mix_FreeChunk(m_sounds[soundIter]);
  }
}


void
Tanks::
Resources::
SoundLoader::
Load(std::string argDir)
{
  // The order must not be changed
  m_sounds.push_back(Mix_LoadWAV((argDir + "Sounds" + Globals::directorySplitter + "Shoot.wav").c_str()));
  m_sounds.push_back(Mix_LoadWAV((argDir + "Sounds" + Globals::directorySplitter + "Explosion.wav").c_str()));
  m_sounds.push_back(Mix_LoadWAV((argDir + "Sounds" + Globals::directorySplitter + "TankExplosion.wav").c_str()));
  m_sounds.push_back(Mix_LoadWAV((argDir + "Sounds" + Globals::directorySplitter + "Idle.wav").c_str()));
  m_sounds.push_back(Mix_LoadWAV((argDir + "Sounds" + Globals::directorySplitter + "Drive.wav").c_str()));
}


int
Tanks::
Resources::
SoundLoader::
GetFreeChannel()
{
  if(m_freeChannels.empty())
  {
    m_maxChannels += 50;
    Mix_AllocateChannels(m_maxChannels);
    for(int i = m_maxChannels - 1; i >= m_maxChannels - 50; --i)
    {
      m_freeChannels.push_back(i);
    }
  }
  int free = m_freeChannels.back();
  m_freeChannels.pop_back();
  return free;
}


